#!/bin/env python
#-*- encoding:utf-8 -*-
import urllib,urllib2
import json
import time,sys
import hashlib
import socket,os,shutil,re
from httplib import HTTPConnection
reload(sys)
sys.setdefaultencoding('utf-8')
class httpAccess(object):
    def __init__(self):
        self.srvname = socket.gethostname()
        self.Time = time.strftime('%Y%m%d_%H',time.localtime(time.time()))
    def setOpener(self,proxy_srv='127.0.0.1',prot='80'):
    #def setOpener(self,proxy_srv='10.1.5.30',prot='80'):
        setProxy = 'http://' + proxy_srv + ':' + prot
        proxy_handler = urllib2.ProxyHandler({'http':setProxy})
        opener = urllib2.build_opener(proxy_handler)
        return opener
    def cacheSt(self,url):
        startTime   = time.strftime('%Y%m%d_%H:%M:%S',time.localtime(time.time()))
        opener = self.setOpener()
        cachepurge(urlList=url)
        try:
            res = opener.open(url).readlines()
            endTime   = time.strftime('%Y%m%d_%H:%M:%S',time.localtime(time.time()))
            msg = self.srvname + '\t' + startTime + '\t' + endTime + '\t' +url + '\n'
            Path = '/tmp/static/' + self.srvname + '-cacheLog-' + self.Time + '.log'
            writeLog(Path,msg)
        except Exception,s:
            #print 'http Error: %s' % s
            endTime   = time.strftime('%Y%m%d_%H:%M:%S',time.localtime(time.time()))
            msg = self.srvname + '\t' + startTime + '\t' + endTime + '\t' +url + '\t' + 'http Error:'  + str(s)  + '\n'
            Path = '/tmp/static/' + self.srvname + '-errorLog-' + self.Time + '.log'
            writeLog(Path,msg)

    def cacheDy(self,ids,md5):
        startTime   = time.strftime('%Y%m%d_%H:%M:%S',time.localtime(time.time()))
        cachepurge(urlList='http://itiku.speiyou.com/itempool/rest/question/queryQuestionByIds.json?ids=' + ids)
        opener = self.setOpener()
        try:
            uri       = 'http://itiku.speiyou.com/itempool/rest/question/queryQuestionByIds.json'
            query     = 'ids=' + ids
            url       = uri + '?' + query
            headers   ={ 'User-Agent' : 'Cache Squid python', 'Accept' : '*/*', 'Content-type':'text/plain', 'plat':'flex', 'sys':'ics3', 'md5':md5}
            req       = urllib2.Request(url,headers=headers)
            #response  = self.opener.open(req)
            response  = opener.open(req)
            the_page  = response.read()
            #return the_page
            endTime   = time.strftime('%Y%m%d_%H:%M:%S',time.localtime(time.time()))
            msg = self.srvname + '\t' + startTime + '\t' + endTime + '\t' +url + '\n'
            Path = '/tmp/questions/' + self.srvname + '-cacheLog-' + self.Time + '.log'
            writeLog(Path,msg)
        except Exception,n:
            #print 'connect service Error!',n
            #sys.exit('2')
            endTime   = time.strftime('%Y%m%d_%H:%M:%S',time.localtime(time.time()))
            msg = self.srvname + '\t' + startTime + '\t' + endTime + '\t' +url + '\t' + 'http Error:' + str(n) +'\n'
            #print msg
            Path = '/tmp/questions/' + self.srvname + '-errorLog-' + self.Time + '.log'
            writeLog(Path,msg)

def cachepurge(ips='127.0.0.1',urlList=''):
#def cachepurge(ips='10.1.5.30',urlList=''):
    http_headers = {
    "Host" : "CacheRefresher",
    "User-Agent" : "SquidClient",
    "Accept" : "*/*",
    "Accept-Encoding" : "gzip,deflate",}
    try:
        startTime   = time.strftime('%Y%m%d_%H:%M:%S',time.localtime(time.time()))
        conn = HTTPConnection(host=ips,port='80')
        conn.request('PURGE', urlList ,headers = http_headers)
    except Exception,n:
        endTime   = time.strftime('%Y%m%d_%H:%M:%S',time.localtime(time.time()))
        msg = self.srvname + '\t' + startTime + '\t' + endTime + '\t' + urlList + '\t' + 'http Error:' + n +'\n'
        Path = '/tmp/questions/' + self.srvname + '-errorLog-' + self.Time + '.log'
        writeLog(Path,msg)

def mkMd5Str(uname='',upwd='',Ukey='',Plat='',Sysplat='',ids='',Skey=''):
    Str     = uname+upwd+Ukey+ids+Plat+Sysplat+Skey
    md5Str  = hashlib.md5(Str.encode('utf-8')).hexdigest()
    return md5Str,ids

def findFile(path):
    Flist = list()
    for i in os.listdir(path):
        if os.path.isfile('/'.join((path,i))):
            Flist.append('/'.join((path,i)))
    return Flist

def mvFile(oldPath):
    otmp = str()
    ntmp = str()
    newPath = str()
    for i in oldPath:
        otmp = otmp + '/' + i
        if re.search('queue',i):
            i = 'finished'
            #print i
        ntmp = ntmp + '/' + i
    old = otmp
    new = ntmp
    for i in range(len(oldPath)-1):
        #print i,oldPath[i]
        if re.search('queue',oldPath[i]):
            newPath = newPath + '/' + 'finished'
        else:
            newPath = newPath + '/' + oldPath[i]
    if not os.path.isdir(newPath):
        os.mkdir(newPath)
    shutil.move(old,new)

def writeLog(Path,msg):
    try:
        logFile = open(Path,'a')
        logFile.write(msg)
        logFile.flush()
        logFile.close()
    except Exception,e:
        dirPath = structure('/tmp/error/log')
        dirMk = str()
        for i in dirPath:
            dirMk = dirMk + '/' + i
            if not (os.path.isdir(dirMk)):
                os.mkdir(dirMk)
        writeLog('/tmp/error/log/error.log','Error:' + str(e))

def structure(path):
    resList = list()
    pathList = path.split('/')
    for  i in pathList:
        if i:
            resList.append(i)
    return resList

def main():
    plat     = 'flex'
    sysplat  = 'ics3'
    ukey     = 'itempool.hanxiao'
    skey     = 'com.xes.itempool'
    flushObj = httpAccess()
    quesQueue = findFile ('/tmp/questions/queue')
    #print quesQueue
    for i in quesQueue:
        quesFile = open(i,'r')
        IdsList = quesFile.readlines()
        quesFile.close()
        for j in IdsList:
            #print j[:-1]
            md5Str,id = mkMd5Str(Plat=plat,Sysplat=sysplat,ids=j[:-1],Skey=skey)
            #print md5Str
            flushObj.cacheDy(ids=id,md5=md5Str)
        mvFile(structure(i))
    staticQueue = findFile('/tmp/static/queue')
    for v in staticQueue:
        staticFile = open(v,'r')
        urlsList = staticFile.readlines()
        staticFile.close()
        for n in urlsList:
            flushObj.cacheSt(n)
        mvFile(structure(v))

if __name__ == '__main__':
    main()
